<?php
include 'template/header.html';
require_once 'lib/function.php';

    $action = isset($_GET['action'])?$_GET['action']:'list';
    switch($action){
        case 'add_check':
            $name = $_POST['name'];
            $age = $_POST['age'];
            $sex = $_POST['sex'];
            $tel = $_POST['tel'];
            $email = $_POST['email'];
            $date = date("Y-m-d H:i:s");
            $sql = "insert into employee values('','$name','$sex','$age','$tel','$date','$email')";
            if(dclData($sql)>0){
                echo "<script>alert('添加成功');location.href='e_action.php';</script>";
            }
            echo "<script>alert('添加失败');location.href='e_action.php';</script>";
            break;
        case 'add':

            include 'template/employee_add.html';

            break;
        case 'list':
            $sql = "select * from employee";
            $rowNum = getRowNum($sql);
            $page = isset($_GET['page'])?$_GET['page']:1;
            $listNum = 5;
            $maxPage = ceil($rowNum/$listNum);
            $rows = page($page,$sql, $listNum);
            // var_dump($rows);
            include 'template/employee_list.html';
            break;
        case 'delete':
            $c_id = $_GET['c_id'];
            $sql = "delete from employee where e_id=$c_id";
            if(dclData($sql)){
                echo "<script>alert('删除成功');location.href='e_action.php'</script>";
            }
            echo "<script>alert('删除失败');location.href='e_action.php'</script>";
            break;
        case 'update':
        $c_id = $_GET['c_id'];
        $sql = "select * from employee where e_id='$c_id'";
        $res = getAllData($sql);
        $res = $res[0];
        include 'template/employee_add.html';
            break;
        case 'update_check':
            $e_id = $_GET['e_id'];
            $name = $_POST['name'];
            $age = $_POST['age'];
            $sex = $_POST['sex'];
            $tel = $_POST['tel'];
            $email = $_POST['email'];
            $date = date("Y-m-d H:i:s");
            $sql = "update employee set e_name='$name',e_sex='$sex',e_age='$age',e_tel='$tel',e_email='$email' where e_id='$e_id'";
            if(dclData($sql)>0){
                echo "<script>alert('更新成功');location.href='e_action.php'</script>";
            }
            echo "<script>alert('更新失败');location.href='e_action.php'</script>";

            break;
    }

    include 'template/foot.html';